﻿Imports System.web.Compilation

Partial Class WebForm_ManageProgram_AddProgram
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.fv.InsertItemTemplate = Me.fv.ItemTemplate
        Me.fv.EditItemTemplate = Me.fv.ItemTemplate
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    End Sub

    Protected Sub ddlReferrer_Databound(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim ddlReferrer As DropDownList = TryCast(sender, DropDownList)

        ddlReferrer.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())
    End Sub

    Protected Sub fv_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewCommandEventArgs) Handles FV.ItemCommand
        If e.CommandName = "Save" Then
            If Me.SaveData() = True Then
                RaiseEvent Saved(Me, New EventArgs)
            End If
        ElseIf e.CommandName = "Canceling" Then
            RaiseEvent Canceled(Me, New EventArgs)
        End If

    End Sub

    Protected Sub fv_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FV.DataBound
        If Me.fv.CurrentMode = FormViewMode.Edit Then
            Me.BindDDLParentProgram()
            TryCast(Me.fv.FindControl("lblItemType"), Label).Visible = False
            TryCast(Me.fv.FindControl("ddlItemType"), DropDownList).Visible = False
            TryCast(Me.fv.FindControl("txtProgramGroupName"), TextBox).Visible = False
            'TryCast(Me.fv.FindControl("ddlTarget"), DropDownList).Enabled = False

            'If Not TypeOf DataBinder.Eval(Me.fv.DataItem, "ReferrerID") Is DBNull Then
            '    TryCast(Me.fv.FindControl("ddlReferrer"), DropDownList).SelectedValue = DataBinder.Eval(Me.fv.DataItem, "ReferrerID")
            'End If
        ElseIf Me.fv.CurrentMode = FormViewMode.Insert Then
            Me.BindDDLProgramUserItem()
            TryCast(Me.fv.FindControl("ddlProgramGroup"), DropDownList).Enabled = True
        End If

        Me.BindDDLProgramVisible()
        Me.BindDDLProgramTarget()

        If Me.fv.CurrentMode = FormViewMode.Insert Then
        Else
            TryCast(Me.fv.FindControl("ddlProgramGroup"), DropDownList).SelectedValue = DataBinder.Eval(Me.fv.DataItem, "ProgramGroup")
            TryCast(Me.fv.FindControl("ddlVisible"), DropDownList).SelectedValue = DataBinder.Eval(Me.fv.DataItem, "Visible")
            TryCast(Me.fv.FindControl("ddlTarget"), DropDownList).SelectedValue = DataBinder.Eval(Me.fv.DataItem, "Target")
        End If
    End Sub

    Protected Sub ddlItemType_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        If TryCast(Me.fv.FindControl("ddlItemType"), DropDownList).SelectedValue = "2" Then
            'txtProgramGroupName
            TryCast(Me.fv.FindControl("txtProgramGroupName"), TextBox).Visible = True
            TryCast(Me.fv.FindControl("txtProgramGroupName"), TextBox).Text = TryCast(Me.fv.FindControl("ddlProgramGroup"), DropDownList).SelectedItem.Text _
                                                                            & "-" _
                                                                            & TryCast(Me.fv.FindControl("txtProgramName"), TextBox).Text
        Else
            TryCast(Me.fv.FindControl("txtProgramGroupName"), TextBox).Visible = False
        End If
    End Sub
End Class

Partial Class WebForm_ManageProgram_AddProgram


    Public Sub Edit(ByVal ProgeamID As String)
        FV.ChangeMode(FormViewMode.Edit)

        Me.SelectedProgramID = ProgeamID

        Dim sql As New StringBuilder
        Dim dat As New clsData
        Dim dt As New System.Data.DataTable

        sql.AppendFormat("select a.*,b.ProgramID As ProgramGroup, ParentEditable = b.Editable  ")
        sql.AppendFormat("from Program a inner join Program b ")
        sql.AppendFormat("on a.parent = b.programid ")
        ' sql.AppendFormat("left join ProgramReferrerMapping prm on a.ProgramID = prm.ProgramID ")
        'sql.AppendFormat("where a.ProgramType = 'Protected' ")
        sql.AppendFormat("where 1=1 ")

        sql.AppendFormat("and a.ProgramID = '{0}' ", ProgeamID)

        dt = dat.GetData(sql)

        Me.fv.DataSource = dt
        Me.fv.DataBind()

        '若為使用者自訂區，則可修改 '群組' 及 'URL'
        'If Me.CheckIsUesrEdit(dt.Rows(0)("Parent").ToString()) Then
        'If dt.Rows(0)("Editable") = "Y" And dt.Rows(0)("ParentEditable") = "Y" And String.IsNullOrEmpty(dt.Rows(0)("Url")) = False Then
        If dt.Rows(0)("Editable") = "Y" Then
            'If Me.CheckIsUesrEdit(dt.Rows(0)("UserEditable").ToString()) Then
            '修改群組

            'Me.BindDDLProgramUserItem()
            TryCast(Me.fv.FindControl("ddlProgramGroup"), DropDownList).Enabled = True
            '修改URL
            TryCast(Me.fv.FindControl("txtUrl"), TextBox).Enabled = True
        Else
            TryCast(Me.fv.FindControl("txtUrl"), TextBox).Enabled = False
        End If


    End Sub

    Private Function CheckIsUesrEdit(ByVal ParentID As String) As Boolean

        Dim sql As New StringBuilder
        Dim dat As New clsData
        Dim dt As New System.Data.DataTable

        sql.AppendFormat("select ProgramGroupID from ProgramGroup Where UserEditable='Y'")
        dt = dat.GetData(sql)

        For i As Integer = 0 To dt.Rows.Count - 1
            If ParentID = dt.Rows(i)(0).ToString() Then
                Return True
            End If
        Next

        Return False

    End Function

    Private Sub BindDDLParentProgram()
        TryCast(Me.fv.FindControl("ddlProgramGroup"), DropDownList).DataTextField = "ProgramName"
        TryCast(Me.fv.FindControl("ddlProgramGroup"), DropDownList).DataValueField = "ProgramID"
        TryCast(Me.fv.FindControl("ddlProgramGroup"), DropDownList).DataSource = MyObj.GetDDLParentProgram()
        TryCast(Me.fv.FindControl("ddlProgramGroup"), DropDownList).DataBind()
    End Sub

    Private Sub BindDDLProgramVisible()
        TryCast(Me.fv.FindControl("ddlVisible"), DropDownList).DataTextField = "Text"
        TryCast(Me.fv.FindControl("ddlVisible"), DropDownList).DataValueField = "Value"
        TryCast(Me.fv.FindControl("ddlVisible"), DropDownList).DataSource = MyObj.GetDDLProgramVisiable()
        TryCast(Me.fv.FindControl("ddlVisible"), DropDownList).DataBind()
    End Sub

    Private Sub BindDDLProgramTarget()
        TryCast(Me.fv.FindControl("ddlTarget"), DropDownList).DataTextField = "Text"
        TryCast(Me.fv.FindControl("ddlTarget"), DropDownList).DataValueField = "Value"
        TryCast(Me.fv.FindControl("ddlTarget"), DropDownList).DataSource = MyObj.GetDDLProgramTarget()
        TryCast(Me.fv.FindControl("ddlTarget"), DropDownList).DataBind()
    End Sub

    Private Sub BindDDLProgramUserItem()
        TryCast(Me.fv.FindControl("ddlProgramGroup"), DropDownList).DataTextField = "ProgramName"
        TryCast(Me.fv.FindControl("ddlProgramGroup"), DropDownList).DataValueField = "ProgramGroupID"
        TryCast(Me.fv.FindControl("ddlProgramGroup"), DropDownList).DataSource = MyObj.GetDDLInsertParentProgram()
        TryCast(Me.fv.FindControl("ddlProgramGroup"), DropDownList).DataBind()
    End Sub

    Public Sub SetInsert()
        FV.ChangeMode(FormViewMode.Insert)

        'TryCast(Me.fv.FindControl(""), Label).Visible = False


    End Sub

    Protected Function SaveData() As Boolean
        Dim sql As New MyStringBuilder
        '  Dim sql2 As New MyStringBuilder
        Dim dat As New clsData
        Dim ss As String = ""
        Using scope As New Transactions.TransactionScope
            If Me.fv.CurrentMode = FormViewMode.Insert Then

                '若選擇目錄，還要再新增到 ProgramGroup
                If TryCast(Me.fv.FindControl("ddlItemType"), DropDownList).SelectedValue = "2" Then
                    sql.AppendFormat("Insert into Program(ProgramType,ProgramID,ProgramName,Parent,Url,TableName,Sequence,Active,Visible,Editable,Target,CanAccessDirectOrganization) ")
                    sql.AppendFormat("select ")
                    sql.AppendFormat("'Protected' ")
                    sql.AppendFormat(",'{0}' ", MyObj.GetMaxProgramID(TryCast(Me.fv.FindControl("ddlProgramGroup"), DropDownList).SelectedValue))
                    sql.AppendFormat(",'{0}' ", TryCast(Me.fv.FindControl("txtProgramGroupName"), TextBox).Text)
                    sql.AppendFormat(",'{0}' ", TryCast(Me.fv.FindControl("ddlProgramGroup"), DropDownList).SelectedValue)
                    sql.AppendFormat(",'{0}' ", TryCast(Me.fv.FindControl("txtUrl"), TextBox).Text)
                    sql.AppendFormat(",'' ")
                    sql.AppendFormat(",'{0}' ", TryCast(Me.fv.FindControl("txtSequence"), TextBox).Text)
                    sql.AppendFormat(",'Y' ")
                    sql.AppendFormat(",'{0}' ", TryCast(Me.fv.FindControl("ddlVisible"), DropDownList).SelectedValue)
                    sql.AppendFormat(",'Y' ")
                    sql.AppendFormat(",'{0}' ", TryCast(Me.fv.FindControl("ddlTarget"), DropDownList).SelectedValue)
                    sql.AppendFormat(",'{0}'; ", "N")
                    sql.AppendFormat("Insert Into ProgramAuthDefinition(ProgramID,AuthName,CreateDate) ")
                    sql.AppendFormat("Select '{0}', ", MyObj.GetMaxProgramID(TryCast(Me.fv.FindControl("ddlProgramGroup"), DropDownList).SelectedValue))
                    sql.AppendFormat("'Admin',  ")
                    sql.AppendFormat("getdate(); ")
                    sql.AppendFormat("Insert into ProgramGroup(ProgramGroupID,ProgramGroupName,ListType,UserEditable) select ")
                    sql.AppendFormat("'{0}' ", MyObj.GetMaxProgramID(TryCast(Me.fv.FindControl("ddlProgramGroup"), DropDownList).SelectedValue))
                    sql.AppendFormat(",'{0}' ", TryCast(Me.fv.FindControl("txtProgramGroupName"), TextBox).Text)
                    sql.AppendFormat(",'T' ")
                    sql.AppendFormat(",'Y'; ")

                    dat.AccData(sql)
                    'dat.AccData(sql2)
                Else
                    Dim programID As Integer = MyObj.GetMaxProgramID(TryCast(Me.fv.FindControl("ddlProgramGroup"), DropDownList).SelectedValue)

                    sql.AppendFormat("Insert into Program(ProgramType,ProgramID,ProgramName,Parent,Url,TableName,Sequence,Active,Visible,Editable,Target,CanAccessDirectOrganization) ")
                    sql.AppendFormat("select ")
                    sql.AppendFormat("'Protected' ")
                    sql.AppendFormat(",'{0}' ", programID)
                    sql.AppendFormat(",'{0}' ", TryCast(Me.fv.FindControl("txtProgramName"), TextBox).Text)
                    sql.AppendFormat(",'{0}' ", TryCast(Me.fv.FindControl("ddlProgramGroup"), DropDownList).SelectedValue)
                    sql.AppendFormat(",'{0}' ", TryCast(Me.fv.FindControl("txtUrl"), TextBox).Text)
                    sql.AppendFormat(",'' ")
                    sql.AppendFormat(",'{0}' ", MyObj.GetMaxSequence(TryCast(Me.fv.FindControl("ddlProgramGroup"), DropDownList).SelectedValue))
                    sql.AppendFormat(",'Y' ")
                    sql.AppendFormat(",'{0}' ", TryCast(Me.fv.FindControl("ddlVisible"), DropDownList).SelectedValue)
                    sql.AppendFormat(",'Y' ")
                    sql.AppendFormat(",'{0}' ", TryCast(Me.fv.FindControl("ddlTarget"), DropDownList).SelectedValue)
                    sql.AppendFormat(",'{0}'; ", "N")
                    sql.AppendFormat("Insert Into ProgramAuthDefinition(ProgramID,AuthName,CreateDate) ")
                    sql.AppendFormat("Select '{0}', ", programID)
                    sql.AppendFormat("'Admin',  ")
                    sql.AppendFormat("getdate(); ")
                    dat.AccData(sql)

                    Me.SelectedProgramID = programID

                    'sql.Length = 0

                    'If TryCast(Me.fv.FindControl("ddlReferrer"), DropDownList).SelectedValue <> "" Then
                    '    sql.AppendFormat("delete ProgramReferrerMapping where ProgramID = @ProgramID ")
                    '    sql.AppendFormat("insert ProgramReferrerMapping (ProgramID, ReferrerID) ")
                    '    sql.AppendFormat("select @ProgramID, @ReferrerID ")

                    '    sql.AddWithValue("ProgramID", Me.SelectedProgramID)
                    '    sql.AddWithValue("ReferrerID", TryCast(Me.fv.FindControl("ddlReferrer"), DropDownList).SelectedValue)

                    '    dat.AccData(sql)
                    'Else
                    '    sql.AppendFormat("delete ProgramReferrerMapping where ProgramID = @ProgramID ")
                    '    sql.AddWithValue("ProgramID", Me.SelectedProgramID)

                    '    dat.AccData(sql)
                    'End If
                End If

            ElseIf Me.fv.CurrentMode = FormViewMode.Edit Then
                sql.AppendFormat("update Program ")
                sql.AppendFormat("set ProgramName='{0}' ", TryCast(Me.fv.FindControl("txtProgramName"), TextBox).Text)
                sql.AppendFormat(",Url='{0}' ", TryCast(Me.fv.FindControl("txtUrl"), TextBox).Text)
                sql.AppendFormat(",Visible='{0}' ", TryCast(Me.fv.FindControl("ddlVisible"), DropDownList).SelectedValue)
                sql.AppendFormat(",Parent='{0}' ", TryCast(Me.fv.FindControl("ddlProgramGroup"), DropDownList).SelectedValue)
                sql.AppendFormat(",Sequence = '{0}' ", TryCast(Me.fv.FindControl("txtSequence"), TextBox).Text)
                'sql.AppendFormat(",Internal = '{0}' ", TryCast(Me.fv.FindControl("ddlInternal"), DropDownList).SelectedValue)
                sql.AppendFormat("where ProgramID = '{0}' ", Me.SelectedProgramID())
                'sql.AppendFormat("and ProgramType = 'Protected' ")
                dat.AccData(sql)

                'If TryCast(Me.fv.FindControl("ddlReferrer"), DropDownList).SelectedValue <> "" Then
                '    sql.AppendFormat("delete ProgramReferrerMapping where ProgramID = @ProgramID ")
                '    sql.AppendFormat("insert ProgramReferrerMapping (ProgramID, ReferrerID) ")
                '    sql.AppendFormat("select @ProgramID, @ReferrerID ")

                '    sql.AddWithValue("ProgramID", Me.SelectedProgramID)
                '    sql.AddWithValue("ReferrerID", TryCast(Me.fv.FindControl("ddlReferrer"), DropDownList).SelectedValue)

                '    dat.AccData(sql)
                'Else
                '    sql.AppendFormat("delete ProgramReferrerMapping where ProgramID = @ProgramID ")
                '    sql.AddWithValue("ProgramID", Me.SelectedProgramID)

                '    dat.AccData(sql)
                'End If
            End If
            scope.Complete()
        End Using

        Return True
    End Function

    Public Property SelectedProgramID() As String
        Get
            Return ViewState("SelectedProgramID")
        End Get
        Set(ByVal value As String)
            ViewState("SelectedProgramID") = value
        End Set
    End Property

    Public Event Saved(ByVal source As Object, ByVal e As EventArgs)
    Public Event Canceled(ByVal source As Object, ByVal e As EventArgs)

End Class